<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class NewPageCommand extends Command
{

    protected static $defaultName = 'new:page';

    protected function configure()
    {
        $this
            ->setDescription('创建App空白页面')
            ->addArgument('dir', InputArgument::OPTIONAL, 'dir name')
            ->addArgument('name', InputArgument::OPTIONAL, 'page name')
            ->addArgument('overwrite', InputArgument::OPTIONAL, 'overwrite')
//            ->addOption('option1', null, InputOption::VALUE_NONE, '所在目录')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $name = $input->getArgument('name');
        $dir = $input->getArgument('dir');
        $overwrite = $input->getArgument('overwrite');

        if ($dir) {
            // ...
            $io->note(sprintf('目录: %s', $dir));
        }else{
            $io->error("请输入目录");
            return 0;
        }

        if ($name) {
            $io->note(sprintf('名称: %s', $name));
        }else{
            $io->error("请输入名称");
            return 0;
        }


        $err = $this->do($dir,$name,$overwrite);
        if ($err) {
            $io->error($err);
        }

        $io->success("OK");
        return 1;
    }

    public function do($dir,$pageName,$overwrite = false)
    {
        if (strpos(__DIR__,":")) {
            //windows
            $seperator = "\\";
        }else{
            //linux
            $seperator = "/";
        }


        $root = str_replace("src{$seperator}Command","",__DIR__);//E:\www\symfony4\src\Command
//        echo __DIR__;
//        echo "\n";
//        echo $root;
//        $root = E:\www\symfony4\

        $cssPath = $root."public/ApiCloud/$dir/$pageName/$pageName.css";
        $cssDir = $root."public/ApiCloud/$dir/$pageName/";
        is_dir($cssDir) OR mkdir($cssDir, 0777, true);
        if (!file_exists($cssPath) || $overwrite) {
            file_put_contents($cssPath,"");
        }
        $jsPath = $root."public/ApiCloud/$dir/$pageName/$pageName.js";
        $jsContent = 'var windowName = "'.$pageName.'";
var Page;
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show")
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
    },{
        //页面方法
        "init":function () {
        }
    })
    Page.init()
}';
        if (!file_exists($jsPath) || $overwrite) {
            file_put_contents($jsPath,$jsContent);
        }
        $tplDir = ucfirst($dir);
        $tplPath = $root."templates/ApiCloud/$tplDir/$pageName.php.twig";
        $tplContent = '{% extends \'ApiCloud/Base/layout.common.html.twig\' %}

{% block body %}
{% endblock %}';
        $tplDir = $root."templates/ApiCloud/";
        is_dir($tplDir) OR mkdir($tplDir, 0777, true);

        if (!file_exists($tplPath) || $overwrite) {
            try {
                file_put_contents($tplPath, $tplContent);
            } catch (\Exception $exception) {
                return $exception->getMessage();
            }
        }
    }
}
